package com.xyy.saas.payment.controller;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.pojo.bo.MenuBo;
import com.xyy.saas.payment.pojo.bo.QueryAccountBo;
import com.xyy.saas.payment.pojo.vo.ResultIntVo;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.dao.model.MenuInfoPo;
import com.xyy.saas.payment.service.MenuInfoService;
import com.xyy.saas.payment.service.auth.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单权限
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class MenuPermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MenuInfoService menuInfoService;

    /** 更新权限接口 */
    @PostMapping(value = "/update",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(@RequestBody String json){
        log.info("调用修改菜单权限接口参数:{}",json);
        ResultVO result = permissionService.update(json);
        return JSON.toJSONString(convertToInt(result));
    }

    private ResultIntVo convertToInt(ResultVO resultVo){
        ResultIntVo intVo = new ResultIntVo();
        intVo.setCode(Integer.valueOf(resultVo.getCode()));
        intVo.setMsg(resultVo.getMsg());
        intVo.setResult(resultVo.getResult());
        return intVo;
    }

    /** 获取所有菜单 */
    @GetMapping(value = "/getAllMenu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getAllMenu(){
        ResultVO result = new ResultVO();
        try {
            List<MenuInfoPo> allMenu = menuInfoService.getAllMenu();
            List<MenuBo> totalMennu = convertPoToBo(allMenu);
            List<MenuBo> menuBos = groupMenu(totalMennu);
            QueryAccountBo bo = new QueryAccountBo();
            bo.setMenuPermission(menuBos);
            result.setCode("0");
            result.setMsg("success");
            result.setResult(bo);
        }catch (Exception e){
            log.error("获取所有菜单异常:{}",e);
            result = ResultVO.createError("获取菜单异常,请联系管理员");
            result.setCode("1");
        }
        return JSON.toJSONString(convertToInt(result));
    }

    private List<MenuBo> groupMenu(List<MenuBo> menuBos){
        List<MenuBo> finalMenu = new ArrayList<>();
        Map<String, List<MenuBo>> groupBos = menuBos.stream().filter((MenuBo item)->item.getParentId() != null).collect(Collectors.groupingBy(MenuBo::getParentId));
        for (MenuBo item:menuBos) {
            if(CollectionUtils.isNotEmpty(groupBos.get(item.getId()))){
                item.setChildren(groupBos.get(item.getId()));
            }
            if (StringUtils.isEmpty(item.getParentId())){
                finalMenu.add(item);
            }
        }
        return finalMenu;
    }

    private List<MenuBo> convertPoToBo(List<MenuInfoPo> menuInfoPos){
        List<MenuBo> menuBos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(menuInfoPos)){
            menuInfoPos.forEach(po -> {
                MenuBo bo = new MenuBo();
                bo.setId(po.getMenuId());
                bo.setName(po.getName());
                bo.setParentId(po.getParentId());
                bo.setUrl(po.getUrl());
                menuBos.add(bo);
            });
        }
        return menuBos;
    }
}
